/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Frontera;

import DAO.ClienteDAO;
import DAO.SolicitudDAO;
import Entidad.Cliente;
import Entidad.Solicitud;
import control.ConexionDB;
import control.ControlCliente;
import java.sql.Connection;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Juan
 */
public class FBuscarSolicitudes extends javax.swing.JFrame {

    /**
     * Creates new form FConsultaConsolidados
     */
    public FBuscarSolicitudes() {
        initComponents();
        cargarClientes();
        this.setLocationRelativeTo(null);
        SFechaInicial.setEditor(new JSpinner.DateEditor(SFechaInicial, "dd/MMMMM/yyyy"));
        SFechaFinal.setEditor(new JSpinner.DateEditor(SFechaFinal, "dd/MMMMM/yyyy"));
        SNumero.setValue(1);
    }

 

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        CBNumero = new javax.swing.JCheckBox();
        CBNumeroMuestras = new javax.swing.JCheckBox();
        CkBCliente = new javax.swing.JCheckBox();
        CBLugarOrigen = new javax.swing.JCheckBox();
        CkBControlCobros = new javax.swing.JCheckBox();
        CBFecha = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        SFechaInicial = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        SFechaFinal = new javax.swing.JSpinner();
        TFLugarOrigen = new javax.swing.JTextField();
        BBuscar = new javax.swing.JButton();
        BCancelar = new javax.swing.JButton();
        SNumero = new javax.swing.JSpinner();
        CBCliente = new javax.swing.JComboBox();
        SNumeroMuestras = new javax.swing.JSpinner();
        CBControlCobros = new javax.swing.JComboBox();
        CBConsultarTodas = new javax.swing.JCheckBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        TResultados = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consultar Solicitudes");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Consultar Solicitudes"));

        jLabel2.setText("Filtar por:");

        CBNumero.setText("Número:");
        CBNumero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBNumeroActionPerformed(evt);
            }
        });

        CBNumeroMuestras.setText("Número total de muestras:");
        CBNumeroMuestras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBNumeroMuestrasActionPerformed(evt);
            }
        });

        CkBCliente.setText("Cliente:");
        CkBCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CkBClienteActionPerformed(evt);
            }
        });

        CBLugarOrigen.setText("Lugar de Origen:");
        CBLugarOrigen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBLugarOrigenActionPerformed(evt);
            }
        });

        CkBControlCobros.setText("Control de Cobros:");
        CkBControlCobros.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CkBControlCobrosActionPerformed(evt);
            }
        });

        CBFecha.setText("Fecha:");
        CBFecha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBFechaActionPerformed(evt);
            }
        });

        jLabel3.setText("Inicial:");

        SFechaInicial.setModel(new javax.swing.SpinnerDateModel());
        SFechaInicial.setEnabled(false);

        jLabel4.setText("Final:");

        SFechaFinal.setModel(new javax.swing.SpinnerDateModel());
        SFechaFinal.setEnabled(false);

        TFLugarOrigen.setEnabled(false);

        BBuscar.setText("Buscar");
        BBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BBuscarActionPerformed(evt);
            }
        });

        BCancelar.setText("Cancelar");
        BCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelarActionPerformed(evt);
            }
        });

        SNumero.setEnabled(false);

        CBCliente.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Cargando lista de clientes>" }));
        CBCliente.setEnabled(false);

        SNumeroMuestras.setEnabled(false);

        CBControlCobros.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Por facturar", "Facturada" }));
        CBControlCobros.setEnabled(false);

        CBConsultarTodas.setText("Consultar Todas");
        CBConsultarTodas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBConsultarTodasActionPerformed(evt);
            }
        });

        TResultados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Consecutivo de solicitud", "Nombre del cliente", "Fecha de radicacion", "Hora de radicacion", "Registro envio", "Recibe", "Control de cobros", "Orden de servicio"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(TResultados);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(CBNumeroMuestras)
                                    .addComponent(CBFecha))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(SFechaInicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(SFechaFinal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addComponent(SNumeroMuestras)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(88, 88, 88))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(CkBCliente)
                                    .addComponent(CBNumero))
                                .addGap(92, 92, 92)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(SNumero)
                                    .addComponent(CBCliente, 0, 181, Short.MAX_VALUE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(CBConsultarTodas)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(CkBControlCobros)
                                    .addComponent(CBLugarOrigen))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(CBControlCobros, 0, 156, Short.MAX_VALUE)
                                    .addComponent(TFLugarOrigen))))
                        .addGap(21, 21, 21))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(BBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jScrollPane1))
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CBNumero)
                    .addComponent(SNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CBLugarOrigen)
                    .addComponent(TFLugarOrigen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CkBCliente)
                    .addComponent(CBCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CkBControlCobros)
                    .addComponent(CBControlCobros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CBNumeroMuestras)
                    .addComponent(SNumeroMuestras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CBConsultarTodas))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CBFecha)
                    .addComponent(jLabel3)
                    .addComponent(SFechaInicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SFechaFinal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BBuscar)
                    .addComponent(BCancelar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CBNumeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBNumeroActionPerformed
        if(CBNumero.isSelected())
        SNumero.setEnabled(true);
        else
        SNumero.setEnabled(false);
    }//GEN-LAST:event_CBNumeroActionPerformed

    private void CkBClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CkBClienteActionPerformed
        if(CkBCliente.isSelected())
        CBCliente.setEnabled(true);
        else
        CBCliente.setEnabled(false);
    }//GEN-LAST:event_CkBClienteActionPerformed

    private void CBNumeroMuestrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBNumeroMuestrasActionPerformed
        if(CBNumeroMuestras.isSelected())
        SNumeroMuestras.setEnabled(true);
        else
        SNumeroMuestras.setEnabled(false);
    }//GEN-LAST:event_CBNumeroMuestrasActionPerformed

    private void CBLugarOrigenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBLugarOrigenActionPerformed
        if(CBLugarOrigen.isSelected())
        TFLugarOrigen.setEnabled(true);
        else
        TFLugarOrigen.setEnabled(false);
    }//GEN-LAST:event_CBLugarOrigenActionPerformed

    private void CkBControlCobrosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CkBControlCobrosActionPerformed
        if(CkBControlCobros.isSelected())
        CBControlCobros.setEnabled(true);
        else
        CBControlCobros.setEnabled(false);
    }//GEN-LAST:event_CkBControlCobrosActionPerformed

    private void CBFechaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBFechaActionPerformed
          if (CBFecha.isSelected()) {
            SFechaInicial.setEnabled(true);
            SFechaFinal.setEnabled(true);
        } else {
            SFechaInicial.setEnabled(false);
            SFechaFinal.setEnabled(false);
        }
    }//GEN-LAST:event_CBFechaActionPerformed

    private void BBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BBuscarActionPerformed
       ConexionDB conexionBaseDeDatos = new ConexionDB();
       Connection conexion = conexionBaseDeDatos.establecerConexion();
        
       ArrayList <String> criterios = new ArrayList();
       ArrayList <Object> valores = new ArrayList();
       solicitudDAO = new SolicitudDAO();
       int idCliente=0;
       int nCriterios = 0;
       
       /*Extraccion de los nombres de los criterios de busqueda y los valores que se pretenden buscar*/
       if(CBNumero.isSelected()){
           criterios.add("consecutivo_de_solicitud"); 
           valores.add(SNumero.getValue());
           nCriterios++;    
       }
       if(CkBCliente.isSelected()){
           /*cargar el cliente para optener la abreviatura, y con ella optener la muestra*/
            try {
                clienteBuscado = clienteDAO.eoncontrarClientePorNombre(CBCliente.getSelectedItem().toString(), conexion);
                idCliente = clienteBuscado.getIdCliente();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
           criterios.add("clientes_id_cliente"); 
           valores.add(idCliente);
           nCriterios++;
       }
       if(CBNumeroMuestras.isSelected()){
           criterios.add("numero_total_muestras"); 
           valores.add(SNumeroMuestras.getValue());
           nCriterios++;
       } 
       if(CBLugarOrigen.isSelected()){
           criterios.add("Lugar_de_origen"); 
           valores.add(TFLugarOrigen.getText().trim());
           nCriterios++;
       }
       if(CkBControlCobros.isSelected()){
           criterios.add("control_cobros"); 
           valores.add(CBControlCobros.getSelectedIndex());
           nCriterios++;
       }
       if(CBFecha.isSelected()){
           criterios.add("fecha_radicacion"+" >"); 
           criterios.add("fecha_radicacion"+" <"); 
           
            SpinnerDateModel modeloFechaInicial = (SpinnerDateModel) SFechaInicial.getModel();
                    SpinnerDateModel modeloFechaFinal = (SpinnerDateModel) SFechaFinal.getModel();
                    java.sql.Date fechaInicial = new java.sql.Date(modeloFechaInicial.getDate().getTime());
                    java.sql.Date fechaFinal = new java.sql.Date(modeloFechaFinal.getDate().getTime());
           
           valores.add(fechaInicial);
           valores.add(fechaFinal);
           nCriterios = nCriterios + 2;
       }
       if(CBConsultarTodas.isSelected()){
           nCriterios=-1;
       }   
       try {
             switch(nCriterios){  
                   case -1: setResultOnTable(solicitudDAO.loadAll(conexion),conexion); break;          
                   case 0: JOptionPane.showMessageDialog(null, "No se ha seleccionado ningún criterio de búsqueda.");break;
                   case 1: setResultOnTable(solicitudDAO.BusquedaUnCriterio(criterios, valores, conexion),conexion); break;                 
                   case 2: setResultOnTable(solicitudDAO.BusquedaDosCriterios(criterios, valores, conexion),conexion); break;
                   case 3: setResultOnTable(solicitudDAO.BusquedaTresCriterios(criterios, valores, conexion),conexion); break; 
                   case 4: setResultOnTable(solicitudDAO.BusquedaCuatroCriterios(criterios, valores, conexion),conexion); break;      
                   case 5: setResultOnTable(solicitudDAO.BusquedaCincoCriterios(criterios, valores, conexion),conexion); break;     
                   case 6: setResultOnTable(solicitudDAO.BusquedaSeisCriterios(criterios, valores, conexion),conexion); break;        
                   case 7: setResultOnTable(solicitudDAO.BusquedaSieteCriterios(criterios, valores, conexion),conexion); break;             
             }
       } catch (SQLException e){
           e.printStackTrace();
       } catch (Exception e){
           e.printStackTrace();
       }
       
    }//GEN-LAST:event_BBuscarActionPerformed

    private void CBConsultarTodasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBConsultarTodasActionPerformed
        if (CBConsultarTodas.isSelected()) {
            SNumero.setEnabled(false);
            CBNumero.setEnabled(false);
            CBNumero.setSelected(false);
            CBCliente.setEnabled(false);
            CkBCliente.setEnabled(false);
            CkBCliente.setSelected(false);
            CBNumeroMuestras.setEnabled(false);
            CBNumeroMuestras.setSelected(false);
            SNumeroMuestras.setEnabled(false);
            CBFecha.setEnabled(false);
            CBFecha.setSelected(false);
            SFechaInicial.setEnabled(false);
            SFechaFinal.setEnabled(false);
            CBLugarOrigen.setEnabled(false);
            CBLugarOrigen.setSelected(false);
            TFLugarOrigen.setEnabled(false);
            CkBControlCobros.setEnabled(false);
            CkBControlCobros.setSelected(false);
            CBControlCobros.setEnabled(false);
        } else {
            CBNumero.setEnabled(true);
            CBNumero.setEnabled(true);
            CkBCliente.setEnabled(true);
            CBNumeroMuestras.setEnabled(true);
            CBFecha.setEnabled(true);
            CBLugarOrigen.setEnabled(true);
            CkBControlCobros.setEnabled(true);
        }    
}//GEN-LAST:event_CBConsultarTodasActionPerformed

    private void BCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_BCancelarActionPerformed
    public void cargarClientes(){
        CBCliente.setEnabled(false);
        CBCliente.setModel(new DefaultComboBoxModel(new Object[]{"Cargando lista de clientes..."}));
        TrabajadorCargarClientes cargarClientes =
                new TrabajadorCargarClientes(controlCliente){
            @Override
            protected void done(){
                try {
                    nombresClientes = get();
                    CBCliente.setModel(new DefaultComboBoxModel(nombresClientes));
                    CBCliente.setSelectedIndex(0);
                } catch (InterruptedException | ExecutionException e){
                    JOptionPane.showMessageDialog(FBuscarSolicitudes.this, e.getMessage(),
                    "Error en hilo trabajador", javax.swing.JOptionPane.ERROR_MESSAGE);
                }
            }
        };  
        cargarClientes.execute();
    }
    private void setResultOnTable(List rs, Connection cnn) throws SQLException{
        resultados = (ArrayList<Solicitud>)rs;
        DefaultTableModel modelo =(DefaultTableModel)TResultados.getModel(); 
        cleanModel(modelo);
        
        if(resultados != null){
             
              for(int i=0; i<resultados.size(); i++){
                   clienteBuscado = optenerCliente(resultados.get(i).getIdCliente(), cnn);
                   modelo.addRow(new Object[]{String.format("SA%04d", resultados.get(i).getConsecutivoSolititud()), 
                   clienteBuscado.getNombre(), resultados.get(i).getFechaRadicacion(), 
                   resultados.get(i).getHoraRadicacion(), resultados.get(i).getRegistroEnvio(),
                   resultados.get(i).getQuienRecibe(), resultados.get(i).getControlCobros(), resultados.get(i).getOrdenServicio() });
              }
                    
        }else{
               JOptionPane.showMessageDialog(null,"No se encontraron resultados.");
        }
       // LResultadosEncontrados.setText(modelo.getRowCount()+" Muestras(s) encontradas.");
        cerrarCnn(cnn);
    }
    
    public Cliente optenerCliente(int idCliente, Connection cnn){    
        try {
              Cliente cliente = new Cliente();
              cliente.setIdCliente(idCliente);
              return clienteDAO.load(cliente,cnn);
            } catch (SQLException ex) {
                ex.printStackTrace();
                return null;
            }
    }
    private void cleanModel(DefaultTableModel modelo){
        int nfilas = modelo.getRowCount();
        for(int i=nfilas-1; i>=0;i--){
        modelo.removeRow(i);
        } 
    }
    public void cerrarCnn(Connection cnn) {
        if (cnn != null) {
            try {
                cnn.close();
            } catch (SQLException e) {
            }
        }
    } 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FBuscarSolicitudes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FBuscarSolicitudes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FBuscarSolicitudes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FBuscarSolicitudes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new FBuscarSolicitudes().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BBuscar;
    private javax.swing.JButton BCancelar;
    private javax.swing.JComboBox CBCliente;
    private javax.swing.JCheckBox CBConsultarTodas;
    private javax.swing.JComboBox CBControlCobros;
    private javax.swing.JCheckBox CBFecha;
    private javax.swing.JCheckBox CBLugarOrigen;
    private javax.swing.JCheckBox CBNumero;
    private javax.swing.JCheckBox CBNumeroMuestras;
    private javax.swing.JCheckBox CkBCliente;
    private javax.swing.JCheckBox CkBControlCobros;
    private javax.swing.JSpinner SFechaFinal;
    private javax.swing.JSpinner SFechaInicial;
    private javax.swing.JSpinner SNumero;
    private javax.swing.JSpinner SNumeroMuestras;
    private javax.swing.JTextField TFLugarOrigen;
    private javax.swing.JTable TResultados;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    private String[] nombresClientes;
    private ControlCliente controlCliente = new ControlCliente();
    private ArrayList<Solicitud> resultados;
    private Cliente clienteBuscado;
    private ClienteDAO clienteDAO = new ClienteDAO();
    private SolicitudDAO solicitudDAO;
}
